Trò chơi Pac-Man

20.566 lượt xem;
1 using System.Collections.Generic;
2 using
UnityEngine;
3
4 public
class GameManager : MonoBehaviour {
5
6     
//--------------------------------------------------------
7     
// Game variables
8
9     
public static int Level = 0;
10     
public static int lives = 3;
11
12     
public enum GameState { Init, Game, Dead, Scores }
13     
public static GameState gameState;
14
15     
private GameObject pacman;
16     
private GameObject blinky;
17     
private GameObject pinky;
18     
private GameObject inky;
19     
private GameObject clyde;
20     
private GameGUINavigation gui;
21
22     
public static bool scared;
23     
static public int score;
24
25     
public float scareLength;
26     
private float _timeToCalm;
27
28     
public float SpeedPerLevel;
29     
30     
//-------------------------------------------------------------------
31     
// singleton implementation
32     
private static GameManager _instance;
33
34     
public static GameManager instance
35     {
36         
get
37         {
38             
if (_instance == null)
39             {
40                 _instance = GameObject.FindObjectOfType<GameManager>();
41                 DontDestroyOnLoad(_instance.gameObject);
42             }
43
44             
return _instance;
45         }
46     }
47
48     
//-------------------------------------------------------------------
49     
// function definitions
50
51     
void Awake()
52     {
53         
if (_instance == null)
54         {
55             _instance =
this;
56             DontDestroyOnLoad(
this);
57         }
58         
else
59         {
60             
if(this != _instance)
61                 Destroy(
this.gameObject);
62         }
63
64         AssignGhosts();
65     }
66
67     
void Start ()
68     {
69         gameState = GameState.Init;
70     }
71
72     
void OnLevelWasLoaded()
73     {
74         
if (Level == 0) lives = 3;
75
76         Debug.Log(
"Level " + Level + " Loaded!");
77         AssignGhosts();
78         ResetVariables();
79
80
81         
// Adjust Ghost variables!
82         clyde.GetComponent<GhostMove>().speed += Level * SpeedPerLevel;
83         blinky.GetComponent<GhostMove>().speed += Level * SpeedPerLevel;
84         pinky.GetComponent<GhostMove>().speed += Level * SpeedPerLevel;
85         inky.GetComponent<GhostMove>().speed += Level * SpeedPerLevel;
86         pacman.GetComponent<PlayerController>().speed += Level*SpeedPerLevel/
2;
87     }
88
89     
private void ResetVariables()
90     {
91         _timeToCalm =
0.0f;
92         scared =
false;
93         PlayerController.killstreak =
0;
94     }
95
96     
// Update is called once per frame
97     
void Update ()
98     {
99         
if(scared && _timeToCalm <= Time.time)
100             CalmGhosts();
101
102     }
103
104     
public void ResetScene()
105     {
106         CalmGhosts();
107
108         pacman.transform.position =
new Vector3(15f, 11f, 0f);
109         blinky.transform.position =
new Vector3(15f, 20f, 0f);
110         pinky.transform.position =
new Vector3(14.5f, 17f, 0f);
111         inky.transform.position =
new Vector3(16.5f, 17f, 0f);
112         clyde.transform.position =
new Vector3(12.5f, 17f, 0f);
113
114         pacman.GetComponent<PlayerController>().ResetDestination();
115         blinky.GetComponent<GhostMove>().InitializeGhost();
116         pinky.GetComponent<GhostMove>().InitializeGhost();
117         inky.GetComponent<GhostMove>().InitializeGhost();
118         clyde.GetComponent<GhostMove>().InitializeGhost();
119
120         gameState = GameState.Init;
121         gui.H_ShowReadyScreen();
122
123     }
124
125     
public void ToggleScare()
126     {
127         
if(!scared) ScareGhosts();
128         
else CalmGhosts();
129     }
130
131     
public void ScareGhosts()
132     {
133         scared =
true;
134         blinky.GetComponent<GhostMove>().Frighten();
135         pinky.GetComponent<GhostMove>().Frighten();
136         inky.GetComponent<GhostMove>().Frighten();
137         clyde.GetComponent<GhostMove>().Frighten();
138         _timeToCalm = Time.time + scareLength;
139
140         Debug.Log(
"Ghosts Scared");
141     }
142
143     
public void CalmGhosts()
144     {
145         scared =
false;
146         blinky.GetComponent<GhostMove>().Calm();
147         pinky.GetComponent<GhostMove>().Calm();
148         inky.GetComponent<GhostMove>().Calm();
149         clyde.GetComponent<GhostMove>().Calm();
150         PlayerController.killstreak =
0;
151     }
152
153     
void AssignGhosts()
154     {
155         
// find and assign ghosts
156         clyde = GameObject.Find(
"clyde");
157         pinky = GameObject.Find(
"pinky");
158         inky = GameObject.Find(
"inky");
159         blinky = GameObject.Find(
"blinky");
160         pacman = GameObject.Find(
"pacman");
161
162         
if (clyde == null || pinky == null || inky == null || blinky == null) Debug.Log("One of ghosts are NULL");
163         
if (pacman == null) Debug.Log("Pacman is NULL");
164
165         gui = GameObject.FindObjectOfType<GameGUINavigation>();
166
167         
if(gui == null) Debug.Log("GUI Handle Null!");
168
169     }
170
171     
public void LoseLife()
172     {
173         lives--;
174         gameState = GameState.Dead;
175     
176         
// update UI too
177         UIScript ui = GameObject.FindObjectOfType<UIScript>();
178         Destroy(ui.lives[ui.lives.Count -
1]);
179         ui.lives.RemoveAt(ui.lives.Count -
1);
180     }
181
182     
public static void DestroySelf()
183     {
184
185         score =
0;
186         Level =
0;
187         lives =
3;
188         Destroy(GameObject.Find(
"Game Manager"));
189     }
190 }


--------------------------------------------------------

Game variables

-------------------------------------------------------------------

singleton implementation

-------------------------------------------------------------------

function definitions

Adjust Ghost variables!

Update is called once per frame

find and assign ghosts

update UI too



Gõ tìm kiếm nhanh...